home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / sbin_-_Static_Binary_Files / IPCHAINS.{3C < prev    next >
Encoding:
Text File  |  1999-09-17  |  2.9 KB  |  130 lines

  1. #! /bin/bash
  2.  
  3. MYVERSION="1.1.1"
  4.  
  5. help()
  6. {
  7.     exec 1>&2
  8.     echo `basename $0` v$MYVERSION: Script to restore firewall chains from stdin.
  9.     echo
  10.     echo "   With the -v option, prints out every rule."
  11.     echo "   With the -f option, clears chains without asking."
  12.     echo "   With the -p option, creates empty referred-to chains."
  13.  
  14.     exit 1
  15. }
  16.  
  17. IPCHAINS=/sbin/ipchains
  18. #IPCHAINS=echo
  19.  
  20. IP_CHAINNAMES_FILE=/proc/net/ip_fwnames
  21. #IP_CHAINNAMES_FILE=ip_fwnames.dummy
  22.  
  23. VERBOSE=0
  24. FORCE=0
  25. AUTOCREATE=0
  26.  
  27. bugreport()
  28. {
  29.     echo "$@"
  30.     echo This is $0 v$MYVERSION
  31.     echo If this is the latest version of ipchains-restore, and the input
  32.     echo was created using the latest version of ipchains-save, then I\'d
  33.     echo really appreciate a bug report.  Please send the input you used,
  34.     echo and all the output from this program to the author,
  35.     echo \`ipchains@rustcorp.com\' with \`BUG-REPORT\' in the subject
  36.     echo line so I know to read the message.
  37.     echo
  38.     echo Apologies for the inconvenience,
  39.     echo Paul \`\`Rusty\'\' Russell.
  40.     exit 1
  41. }
  42.  
  43. try_create()
  44. {
  45.     EXPECT_CHAIN=0
  46.     for arg
  47.     do
  48.     if [ $EXPECT_CHAIN = 1 ]; then
  49.         case "$arg"
  50.         in
  51.         REJECT) ;;
  52.         DENY) ;;
  53.         ACCEPT) ;;
  54.         REDIRECT) ;;
  55.         MASQ) ;;
  56.         *) $IPCHAINS -N $arg 2>/dev/null >/dev/null && if [ $VERBOSE ]; then echo Auto-creating chain $arg; fi
  57.         esac
  58.         return
  59.     elif [ x"$arg" = x"-j" ]; then
  60.         EXPECT_CHAIN=1
  61.     fi
  62.     done
  63. }
  64.  
  65. for arg
  66. do
  67.     case "$arg"
  68.     in
  69.     -v) VERBOSE=1 ;;
  70.     -f) FORCE=1 ;;
  71.     -p) AUTOCREATE=1 ;;
  72.     *) help ;;
  73.     esac
  74. done
  75.  
  76. SKIP=""
  77. while read LINE
  78. do
  79.     case "$LINE"
  80.     in
  81.     :*) CHAIN=`echo $LINE | cut -c2- | cut -d\  -f1`
  82.         SKIP=0
  83.         if [ $CHAIN = input -o $CHAIN = output -o $CHAIN = forward ]
  84.         then
  85.         [ $VERBOSE = 1 ] && echo Setting policy for \`$CHAIN\'.
  86.         POLICY=`echo $LINE | cut -c2- | cut -d\  -f2`
  87.         case "$POLICY" 
  88.         in
  89.         # Old-style (numeric) policies.
  90.             -1) $IPCHAINS -P $CHAIN REJECT ;;
  91.             1) $IPCHAINS -P $CHAIN DENY ;;
  92.             2) $IPCHAINS -P $CHAIN ACCEPT ;;
  93.             3) $IPCHAINS -P $CHAIN REDIRECT ;;
  94.             4) $IPCHAINS -P $CHAIN MASQ ;;
  95.             *) $IPCHAINS -P $CHAIN $POLICY ;;
  96.         esac
  97.         elif grep -q "^$CHAIN " $IP_CHAINNAMES_FILE
  98.         then
  99.         if [ $FORCE = 1 ]; then REPL='f'
  100.         else
  101.             echo -n Chain \`$CHAIN\' already exists.  "Skip or flush? [S/f]? "
  102.             read REPL < /dev/tty
  103.         fi
  104.         case $REPL
  105.         in
  106.             [fF]*) $IPCHAINS -F $CHAIN 
  107.             [ $FORCE = 1 ] || echo Flushing \`$CHAIN\'.
  108.             ;;
  109.             *) SKIP="$SKIP $CHAIN"
  110.             [ $VERBOSE = 1 ] || echo Skipping \`$CHAIN\'. ;;
  111.         esac
  112.         else
  113.         echo Creating chain \`$CHAIN\'.
  114.         $IPCHAINS -N $CHAIN
  115.         fi
  116.         ;;
  117.  
  118.     *)  if echo $SKIP | fgrep -w -q -e `echo $LINE | cut -d' ' -f2`
  119.         then
  120.         [ $VERBOSE = 1 ] && echo SKIPPING $IPCHAINS $LINE 1>&2
  121.         else
  122.         # If line contains -j <chain>, we may need to create it.
  123.         [ $AUTOCREATE = 1 ] && try_create $LINE
  124.         [ $VERBOSE = 1 ] && echo $IPCHAINS $LINE 1>&2
  125.         $IPCHAINS $LINE || bugreport "ipchains command $LINE failed"
  126.         fi
  127.         ;;
  128.     esac
  129. done
  130.